//
//  ViewController.swift
//  TestPan
//
//  Created by YC on 2017/4/11.
//  Copyright © 2017年 YC. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var titleViewTopConstraint: NSLayoutConstraint!
    @IBOutlet weak var scrollView: UIScrollView!
    var panGesture: YCPanGestureRecognizer!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.panGesture = YCPanGestureRecognizer(target: self, action: #selector(ViewController.testPanGesture(_:)))
        
        self.view.addGestureRecognizer(self.panGesture)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBAction func testPanGesture(_ sender: UIPanGestureRecognizer) {

        switch sender.state {
        case .changed:
            let velocity = sender.velocity(in: self.view)
            
            if velocity.x == 0 {
                if velocity.y > 0 {
                    self.titleViewTopConstraint.constant = 113
                }else {
                    self.titleViewTopConstraint.constant = 0
                }
                
                self.view.setNeedsUpdateConstraints()
                self.scrollView.isScrollEnabled = false
                UIView.animate(withDuration: 0.3, animations: { 
                    self.view.layoutIfNeeded()
                }, completion: { (complete) in
                    self.scrollView.isScrollEnabled = true
                })
            }
        default:
            break
        }
        
    }
    
}

